﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;


namespace AdaptiveAgents.Tools
{
    class CSVprinter
    {
        public static void WriteToCSV(String fileName, DataTable dt)
        {
            //set an output writer
            System.IO.TextWriter writer = new System.IO.StreamWriter(fileName);

            //print columns name
            for (int i = 0; i < dt.Columns.Count; ++i)
                writer.Write(dt.Columns[i].ToString() + ",");

            writer.WriteLine();

            //for each row...
            for (int i = 0; i < dt.Rows.Count; ++i)
            {
                DataRow row = dt.Rows[i];
                //for each cell in the row...
                for (int j = 0; j < dt.Columns.Count; ++j)
                    writer.Write(row[j].ToString() + ",");
                writer.WriteLine();
            }

            writer.Close();
        }

        /// <summary>
        /// ReadFromCSV - reads values from csv file, parses it and constructs a DataTable
        /// object. Record delimeter is end line character(s). Field delimeter is delimeter param.
        /// The method assumes that the first line of the file is the names of the columns.
        /// The method returns a DataTable and each field (row,column) is stored as string object.
        /// </summary>
        /// <param name="fileName">the name of the csv file</param>
        /// <param name="delimeter">field delimeter</param>
        /// <returns>DataTable</returns>
        public static DataTable ReadFromCSV(String fileName, char delimeter)
        {
            DataTable dt = new DataTable();
            StreamReader reader = new StreamReader(fileName);
            //read first line = headers
            string headerString = reader.ReadLine();
            if (headerString == null)
            { 
                // the file is empty - return empty dt
                return dt;
            }
            // split the string to array of headers
            string[] headers = headerString.Split(delimeter);
            // add columns to dt
            foreach (string header in headers)
            {
                // considers columns as string columns
                dt.Columns.Add(new DataColumn(header, typeof(string)));
            }
            // read the data
            string line = reader.ReadLine();
            while (line != null)
            {
                string[] data = line.Split(delimeter);
                // create new row
                DataRow dr = dt.NewRow();
                // add the data as string value
                for (int i = 0; i < headers.Length; i++)
                {
                    dr[headers[i]] = data[i];
                }
                // add row
                dt.Rows.Add(dr);
                // read next line
                line = reader.ReadLine();
            }
            // close the reader
            reader.Close();

            return dt;
        }
   
            
    }

}
